<?php

namespace Beerus\Modules\DecoCompany\Actions;

use Doraemon\model\DecoCompany\DecoCompanyAuditLog;
use Doraemon\model\DecoCompanyAuth;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class AuditPass extends RowAction
{
	public $name = '资料通过';
	const TOAST_ALERT = '确认资料审核通过吗？';

	public function handle($id) {
		$result = DecoCompanyAuth::update([
			'datum_status' => DecoCompanyAuth::DATUM_PASS,
			'admin_operate_time' => time(),
			'admin_id' => Admin::user()->id,
			'admin_name' => Admin::user()->username
		], [
			'id' => $id
		]);

		if ($result) {
			$info = DecoCompanyAuth::getById($id);

			DecoCompanyAuditLog::insert([
				'uid' => $info['uid'],
				'op_log' => json_encode([
					'op' => DecoCompanyAuditLog::OP_PASS,
				]),
				'admin_id' => Admin::user()->id,
				'admin_name' => Admin::user()->username
			]);

			return $this->response()->success('审核通过成功(＾－＾)V')->refresh();
		}else{
			return $this->response()->success('审核通过失败,请稍后重试o(TωT)o');
		}
	}

	public function retrieveModel(Request $request)
	{
		if (!$key = $request->get('_key')) {
			return false;
		}

		return $key;
	}

	public function dialog()
	{
		$this->confirm(self::TOAST_ALERT);
	}
}
